/*
*	Copyright (C) 2014  Julio Montes, Mario Carrillo
*	
*	Julio Montes e-mail: imc.coder@gmail.com 
*
*	Mario Carrillo e-mail: mario_90ago@live.com.mx
*
*	BSD 3-Clause License
*/

#include "alliance/include/createfiledlg.h"

#include <wx/wx.h>
#include <wx/tokenzr.h>

namespace alliance
{
	CreateFileDialog::CreateFileDialog( wxWindow *parent )
		: wxDialog( parent, wxID_ANY, wxT( "New file" ), wxDefaultPosition, wxSize( 300 ,200 ) )
	{
		wxBoxSizer *bsMain = new wxBoxSizer( wxVERTICAL );

		wxStaticText *stFileName = new wxStaticText( this, wxID_ANY, wxT( "Filename with extension" ) );
		tcFileName = new wxTextCtrl( this, wxID_ANY, wxT( "" ), wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER );
		tcFileName->SetFocus();
		bsMain->AddSpacer( 20 );
		bsMain->Add( stFileName, 0, wxEXPAND | wxLEFT | wxRIGHT, 20 );
		bsMain->AddSpacer( 5 );
		bsMain->Add( tcFileName, 0, wxEXPAND | wxLEFT | wxRIGHT, 20 );


		wxStaticText *stVirtualFolder = new wxStaticText( this, wxID_ANY, wxT( "Virtual folder" ) );
		tcVirtualFolder = new wxTextCtrl( this, wxID_ANY, wxT( "src/" ), wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER );
		bsMain->AddSpacer( 20 );
		bsMain->Add( stVirtualFolder, 0, wxEXPAND | wxLEFT | wxRIGHT, 20 );
		bsMain->AddSpacer( 5 );
		bsMain->Add( tcVirtualFolder, 0, wxEXPAND | wxLEFT | wxRIGHT, 20 );

		wxBoxSizer *bsButtons = new wxBoxSizer( wxHORIZONTAL );
		wxButton *btnCancel = new wxButton( this, wxID_CANCEL );
		wxButton *btnOk = new wxButton( this, wxID_OK );
		bsButtons->Add( btnCancel );
		bsButtons->AddSpacer( 5 );
		bsButtons->Add( btnOk );
		bsMain->AddSpacer( 20 );
		bsMain->Add( bsButtons, 0 , wxALIGN_RIGHT | wxRIGHT, 20 );

		SetSizer( bsMain );

		Connect( wxID_ANY, wxEVT_COMMAND_TEXT_ENTER, wxCommandEventHandler( CreateFileDialog::OnOk ), NULL, this );
		Connect( wxID_OK, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( CreateFileDialog::OnOk ), NULL, this );
	}

	CreateFileDialog::~CreateFileDialog()
	{

	}

	void CreateFileDialog::OnOk( wxCommandEvent &evt )
	{
		const wxString fileName( tcFileName->GetValue().Trim().Trim( false ) );
		const wxString virtualFolder( tcVirtualFolder->GetValue().Trim().Trim( false ) );

		if( fileName == wxEmptyString )
		{
		    wxMessageBox( wxT( "The name of file is empty" ), wxT( "Error" ), wxICON_ERROR );
		    tcFileName->SetFocus();
		}
		else if( fileName.Find( wxT( '/' ) ) != wxNOT_FOUND || fileName.Find( wxT( '\\' ) ) != wxNOT_FOUND )
		{
		    wxMessageBox( wxT( "File name has a incorrect character (/ or \\)" ), wxT( "Error" ), wxICON_ERROR );
		    tcFileName->SetFocus();
		}
		else
		{
		    EndModal( wxID_OK );
		}
	}

	wxString CreateFileDialog::GetFileName() const
	{
		return tcFileName->GetValue().Trim().Trim( false );
	}

	wxString CreateFileDialog::GetVirtualFolder() const
	{
		wxStringTokenizer stVirtualDir( tcVirtualFolder->GetValue().Trim().Trim( false ), wxT( "/" ), wxTOKEN_STRTOK );
		wxString virtualDir;

		while( stVirtualDir.HasMoreTokens() )
		    virtualDir += stVirtualDir.GetNextToken() + wxT( '/' );

		return virtualDir;
	}
}
